package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.entity.EBuyGoods;
import com.chhuang.ewcrm.entity.EGoods;
import com.chhuang.ewcrm.entity.ESellGoods;
import com.chhuang.ewcrm.service.EBuyGoodsService;
import com.chhuang.ewcrm.service.EGoodsService;
import com.chhuang.ewcrm.service.ESellGoodsService;
import com.chhuang.ewcrm.vo.ResultBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chhuang
 * @create 2020-11-16-14:05
 */
@RestController
@RequestMapping("wapi/goods")
public class WGoodsController {
    Logger logger= LoggerFactory.getLogger(WWarehouseController.class);
    /**
     * 服务对象
     */
    @Resource
    private EBuyGoodsService eBuyGoodsService;
    @Resource
    private ESellGoodsService eSellGoodsService;
    @Resource
    private EGoodsService eGoodsService;

    /**
     * 获取物品
     * @param typeId 0采购物品 1销售物品 2其它物品
     * @param warehouseId 销售的产品
     * @return
     */
    @GetMapping("getGoods")
    public ResultBean<List<EGoods>> getGoods(Integer typeId, String warehouseId){
        ResultBean<List<EGoods>> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);

        if(typeId!=null){
            final List<EGoods> goodsList = new LinkedList<>();
            switch (typeId){
                case 0://采购的原料
                    EBuyGoods buyGoods = new EBuyGoods();
                    buyGoods.setWarehouseId(warehouseId);
                    List<EBuyGoods> buyGoodslist = eBuyGoodsService.query(buyGoods, "create_time");
                    if(buyGoodslist!=null && !buyGoodslist.isEmpty())
                        buyGoodslist.forEach(item->{
                            EGoods eg = new EGoods(item.getGoodsId(),item.getGoodsName(),
                                    item.getWarehouseId(),item.getWarehouseName(),
                                    item.getModifyTime(),item.getCreateTime());
                            goodsList.add(eg);
                        });
                    break;
                case 1://销售的产品
                    ESellGoods sellGoods = new ESellGoods();
                    sellGoods.setWarehouseId(warehouseId);
                    List<ESellGoods> sellGoodslist = eSellGoodsService.query(sellGoods, "create_time");
                    if(sellGoodslist!=null && !sellGoodslist.isEmpty())
                        sellGoodslist.forEach(item->{
                            EGoods eg = new EGoods(item.getGoodsId(),item.getGoodsName(),
                                    item.getWarehouseId(),item.getWarehouseName(),
                                    item.getModifyTime(),item.getCreateTime());
                            goodsList.add(eg);
                        });
                    break;
                default://其它物品
                    EGoods goods = new EGoods();
                    goods.setWarehouseId(warehouseId);
                    List<EGoods> eGoodslist = eGoodsService.query(goods, "create_time");
                    goodsList.addAll(eGoodslist);
            }
            resultBean.setData(goodsList);
            resultBean.setSuccess(true);
        }else {
            resultBean.setMessage("参数不能为空");
        }

        return resultBean;
    }
}
